package cn.edu.nju.software.android.util;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateHelper {
	public static int biggerByDays(Date d1, Date d2){
		return (int) ((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24));
	}

	public static Date parseDate(String string, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		try {
			return sdf.parse(string);
		} catch (ParseException e) {
			return null;
		}
	}

	@SuppressLint("SimpleDateFormat")
	public static String getDateStr(Date date){
		Calendar timeNow = Calendar.getInstance();
		Calendar time = Calendar.getInstance();
		time.setTime(date);
		long subtractedTime = (long) (timeNow.getTimeInMillis() - time.getTimeInMillis());
		if(subtractedTime < 3600000){
			String str = subtractedTime / (1000 * 60) + "分钟前";
			return str;
		}else if(subtractedTime < 24 * 3600000){
			SimpleDateFormat format = new SimpleDateFormat("今天HH:mm");
			return format.format(date);
		}else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return format.format(date);
		}
	}
}
